﻿using Chat_IService;
using Chat_Model;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Chat_Model.ViewModel;
using SqlSugar.Extensions;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;

namespace Chat_WebApi.Controllers.v1
{
    [Route("/api/v1/[controller]")]
    [ApiController]
    [Authorize]
    public class ContactsController : BaseController
    {
        private readonly ILarUsersService _larUsersService;
        private readonly ILarUsersFriendsService _larUsersFriendsService;
        private readonly ILarUsersFriendsApplyService _larUsersFriendsApplyService;
        private readonly IMapper _mapper;
        public ContactsController(ILarUsersService larUsersService,
            ILarUsersFriendsService larUsersFriendsService,
            IMapper mapper,
            ILarUsersFriendsApplyService larUsersFriendsApplyService)
        {
            this._larUsersService = larUsersService;
            this._larUsersFriendsService = larUsersFriendsService;
            this._larUsersFriendsApplyService = larUsersFriendsApplyService;
            this._mapper = mapper;
        }

        #region 联系人
        /// <summary>
        /// 搜索联系人
        /// </summary>
        [HttpGet("Search")]
        //[authour]
        public async Task<MessageModel<object>> Search([FromQuery] string mobile)
        {
            if (string.IsNullOrWhiteSpace(mobile)) return new MessageModel<object>() { Code = 305 };
            var model = await this._larUsersService.GetSingleAsync(x => x.Mobile == mobile);
            return new MessageModel<object>()
            {
                Code = 200,
                Message = "",
                Data = model,
            };
        }
        /// <summary>
        /// 获取联系人列表
        /// </summary>
        [HttpGet("list")]
        public async Task<MessageModel<List<LarUsersFriendsDto>>> List()
        {
            int uid = this._larUsersService.GetUserId();
            var list = await this._larUsersFriendsService.GetContactsList(uid);
            return new MessageModel<List<LarUsersFriendsDto>>()
            {
                Code = 200,
                Message = "success",
                Data = list,
            };
        }
        /// <summary>
        /// 删除联系人
        /// </summary>
        [HttpPost]
        public async Task<MessageModel<object>> Delete()
        {
            return new MessageModel<object>() { };
        }
        /// <summary>
        ///  编辑联系人备注
        /// </summary>
        [HttpPost("edit-remark")]
        public async Task<MessageModel<object>> EditRemark([FromBody] ContactsEditRemarkParam param)
        {
            int uid = this._larUsersService.GetUserId();
            var friendModel = await this._larUsersFriendsService.GetSingleAsync(x => x.FriendId == param.Friend_id && x.UserId == uid);
            friendModel.Remark = param.Remarks;
            this._larUsersFriendsService.Update(friendModel);
            return this.Success();
        }
        #endregion
        #region 好友申请模块


        /// <summary>
        ///  联系人申请列表
        /// </summary>
        [HttpGet("apply-records")]
        public async Task<MessageModel<object>> Records([FromQuery] BaseParam baseParam)
        {
            int uid = this._larUsersService.GetUserId();
            var list = await this._larUsersFriendsApplyService.GetContactsApplyList(uid, baseParam.Page, baseParam.Page_Size);
            return new MessageModel<object>()
            {
                Code = 200,
                Data = new
                {
                    Rows = list
                },
            };
        }
        /// <summary>
        ///  添加联系人申请
        /// </summary>
        [HttpPost("add")]
        public async Task<MessageModel<object>> Create([FromBody] ContactsApplyCreateParam param)
        {
            int uid = this._larUsersService.GetUserId();
            if (param.friend_id == uid)
            {
                return new MessageModel<object>()
                {
                    Code = 305,
                    Message = "不能添加自己为好友...",
                    Data = null,
                };
            }
            LarUsers larUsers = await this._larUsersService.GetSingleAsync(x => x.Id == param.friend_id);
            if (larUsers == null)
                return new MessageModel<object>()
                {
                    Code = 305,
                    Message = "用户不存在...",
                    Data = null,
                };
            if (await this._larUsersFriendsApplyService.InsertAsync(new LarUsersFriendsApply()
            {
                UserId = uid,
                FriendId = larUsers.Id,
                Remark = param.remark,
            }))
                return new MessageModel<object>()
                {
                    Code = 200,
                    Message = "发送好友申请成功...",
                    Data = null,
                };
            else
                return new MessageModel<object>()
                {
                    Code = 305,
                    Message = "添加好友申请失败...",
                    Data = null,
                };
        }
        /// <summary>
        ///  同意联系人申请
        /// </summary>
        [HttpPost("accept-invitation")]
        public async Task<MessageModel<object>> Accept([FromBody] ContactsApplyAcceptPrarm param)
        {
            int uid = this._larUsersService.GetUserId();
            var appModel = await this._larUsersFriendsApplyService.GetSingleAsync(x => x.Id == param.Apply_Id);
            List<LarUsersFriends> list = new List<LarUsersFriends>();
            list.Add(new LarUsersFriends()
            {
                UserId = appModel.FriendId,
                FriendId = appModel.UserId,
                Status = 1,
                Remark = param.Remarks,
            });
            list.Add(new LarUsersFriends()
            {
                UserId = appModel.UserId,
                FriendId = appModel.FriendId,
                Status = 1,
            });
            this._larUsersFriendsService.InsertRange(list);
            return new MessageModel<object>()
            {
                Code = 200,
            };
        }
        /// <summary>
        ///  拒绝联系人申请 
        /// </summary>
        //[HttpPost("apply/decline")]
        [HttpPost("delete-apply")]
        public async Task<MessageModel<object>> Decline([FromBody] ContactsApplyAcceptPrarm param)
        {
            if (this._larUsersFriendsApplyService.Delete(x => x.Id == param.Apply_Id))
                return new MessageModel<object>()
                {
                    Code = 200,
                };
            else
                return new MessageModel<object>()
                {
                    Code = 305,
                };
        }
        /// <summary>
        ///  获取联系人申请未读数
        /// </summary>
        [HttpGet("apply-unread-num")]
        public async Task<MessageModel<object>> ApplyUnreadNum([FromQuery] string apply_id)
        {
            return new MessageModel<object>()
            {
                Code = 200,
                Message = "",
                Data = new
                {
                    unread_num = 0
                },
            };
        }
        #endregion
    }
}
